package com.dykj.fireFighting.server.fire.costmanagement.service.impl;

import com.dykj.fireFighting.api.fire.costmanagement.entity.CostManagement;
import com.dykj.fireFighting.api.fire.costmanagement.service.ICostManagementService;
import com.dykj.fireFighting.api.fire.costmanagement.vo.CostManageMentVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.server.fire.costmanagement.mapper.CostManagementMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 成本管理
 *
 * @Author LIJIANJIE
 * @Date  2021-08-31 13:52:33
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class CostManagementServiceImpl extends BaseServiceImpl<CostManagementMapper, CostManagement> implements ICostManagementService {

    @Override
    public List<CostManageMentVo> getCount(Integer year, Integer unitId, Long wbId) {
        ArrayList<CostManageMentVo> list = new ArrayList<>();
        String beginTime = year + "-01-01 00:00:00";
        String endTime = year + "-12-31 23:59:59";
        List<CostManagement> costManagements;
        if (unitId == null) {
            costManagements = this.baseMapper.getCount(beginTime,endTime,null,wbId);
        }else {
            costManagements = this.baseMapper.getCount(beginTime, endTime, unitId, null);
        }
        Map<Integer, List<CostManagement>> map = costManagements.stream().collect(Collectors.groupingBy(CostManagement::getType));
        for (Map.Entry<Integer, List<CostManagement>> listEntry : map.entrySet()) {
            CostManageMentVo vo = new CostManageMentVo();
            vo.setType(listEntry.getKey());
            for (CostManagement value : listEntry.getValue()) {
                if ("01".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setJanuary(vo.getJanuary() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getJanuary().add(value.getMoney()));
                }else if ("02".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setFebruary(vo.getFebruary() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getFebruary().add(value.getMoney()));
                }else if ("03".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setMarch(vo.getMarch() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getMarch().add(value.getMoney()));
                }else if ("04".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setApril(vo.getApril() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getApril().add(value.getMoney()));
                }else if ("05".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setMay(vo.getMay() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getMay().add(value.getMoney()));
                }else if ("06".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setJune(vo.getJune() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getJune().add(value.getMoney()));
                }else if ("07".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setJuly(vo.getJuly() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getJuly().add(value.getMoney()));
                }else if ("08".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setAugust(vo.getAugust() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getAugust().add(value.getMoney()));
                }else if ("09".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setSeptember(vo.getSeptember() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getSeptember().add(value.getMoney()));
                }else if ("10".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setOctober(vo.getOctober() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getOctober().add(value.getMoney()));
                }else if ("11".equals((value.getCostHappenTime()).substring(5,7))) {
                    vo.setNovember(vo.getNovember() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getNovember().add(value.getMoney()));
                }else if ("12".equals((value.getCostHappenTime()).substring(5,7))){
                    vo.setDecember(vo.getDecember() == null? (new BigDecimal("0")).add(value.getMoney()) : vo.getDecember().add(value.getMoney()));
                }
            }
            list.add(vo);
        }

        return list;
    }
}
